﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Polyline = Autodesk.AutoCAD.DatabaseServices.Polyline;

namespace CADTool.TestTool
{
    internal class ProgressBar
    {
        public static class JinDuTiaodemoclass
        {
            public static void demo()
            {
                //进度条影响运行效率，只在必要的时候使用
                JinDuTiao jd = new JinDuTiao("屏幕瞬态进度条演示");
                for (int i = 0; i < 1000; i++)
                {
                    if (i % 10 == 0)
                    {
                        jd.Run(i, 1000 / 10);//循环10次调用一次进度条；
                        System.Windows.Forms.Application.DoEvents();
                    }
                    Thread.Sleep(100);

                }
                jd.Stop();
            }
        }
        public class JinDuTiao
        {
            TransientManager tm;
            MText mt;
            MText baifen;
            Polyline line;
            Polyline plbox;
            Point3d p1;
            Point3d p2;
            double H, W;
            public JinDuTiao()
            {
                tm = TransientManager.CurrentTransientManager;
                baifen = new MText();
                plbox = new Polyline();
                line = new Polyline();
                var ext = GetViewExtents3d();
                var w = ext.MaxPoint.X - ext.MinPoint.X;
                var h = ext.MaxPoint.Y - ext.MinPoint.Y;
                H = h * 0.02;
                W = w * (1 - 0.618) * 0.618;
                p1 = ext.MinPoint + new Vector3d(w * 0.618, h * 0.1, 0);
                p2 = p1 + new Vector3d(W, 0, 0);
                mt = Mtext("正在运行......", p1 + new Vector3d(0, -H, 0), H);
                mt.Color = Color.FromRgb(0, 255, 0);
                baifen = Mtext("0.0%", p1 + new Vector3d(0, H * 1.6, 0), H * 0.7);
                baifen.Color = Color.FromRgb(0, 255, 0);
                plbox.AddVertexAt(0, p1.Convert2d(new Plane()), 0, 0, 0);
                plbox.AddVertexAt(1, p2.Convert2d(new Plane()), 0, 0, 0);
                plbox.Color = Color.FromRgb(0, 255, 0);
                line.AddVertexAt(0, p1.Convert2d(new Plane()), 0, 0, 0);
                line.AddVertexAt(1, p1.Convert2d(new Plane()), 0, 0, 0);
                line.Color = Color.FromRgb(255, 255, 0);
                line.ConstantWidth = H;

                tm.AddTransient(plbox, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
                tm.AddTransient(mt, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
                tm.AddTransient(baifen, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
                tm.AddTransient(line, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());

            }
            public JinDuTiao(string str)
            {
                tm = TransientManager.CurrentTransientManager;
                baifen = new MText();
                plbox = new Polyline();
                line = new Polyline();
                var ext = GetViewExtents3d();
                var w = ext.MaxPoint.X - ext.MinPoint.X;
                var h = ext.MaxPoint.Y - ext.MinPoint.Y;
                H = h * 0.02;
                W = w * (1 - 0.618) * 0.618;
                p1 = ext.MinPoint + new Vector3d(w * 0.618, h * 0.1, 0);
                p2 = p1 + new Vector3d(W, 0, 0);
                mt = Mtext(str, p1 + new Vector3d(0, -H, 0), H);
                mt.Color = Color.FromRgb(0, 255, 0);
                baifen = Mtext("0.0%", p1 + new Vector3d(0, H * 1.6, 0), H * 0.7);
                baifen.Color = Color.FromRgb(0, 255, 0);
                plbox.AddVertexAt(0, p1.Convert2d(new Plane()), 0, 0, 0);
                plbox.AddVertexAt(1, p2.Convert2d(new Plane()), 0, 0, 0);
                plbox.Color = Color.FromRgb(0, 255, 0);
                line.AddVertexAt(0, p1.Convert2d(new Plane()), 0, 0, 0);
                line.AddVertexAt(1, p1.Convert2d(new Plane()), 0, 0, 0);
                line.Color = Color.FromRgb(255, 255, 0);
                line.ConstantWidth = H;

                tm.AddTransient(plbox, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
                tm.AddTransient(mt, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
                tm.AddTransient(baifen, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
                tm.AddTransient(line, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());

            }

            public void Run(double t, double max)
            {
                var pat = p1 + new Vector3d(t / max * W, 0, 0);
                line.SetPointAt(1, pat.Convert2d(new Plane()));
                baifen.Location = pat + new Vector3d(0, H * 1.6, 0);
                baifen.Contents = (t / max * 100).ToString("0") + "%";
                tm.UpdateTransient(line, new IntegerCollection());
                tm.UpdateTransient(baifen, new IntegerCollection());
                Application.UpdateScreen();
                if (t >= max) { Stop(); }
            }
            public void Run(string str, double t, double max)
            {
                var pat = p1 + new Vector3d(t / max * W, 0, 0);
                line.SetPointAt(1, pat.Convert2d(new Plane()));
                baifen.Location = pat + new Vector3d(0, H * 1.6, 0);
                baifen.Contents = (t / max * 100).ToString("0") + "%";
                mt.Contents = str;
                tm.UpdateTransient(line, new IntegerCollection());
                tm.UpdateTransient(mt, new IntegerCollection());
                tm.UpdateTransient(baifen, new IntegerCollection());
                Application.UpdateScreen();
                if (t >= max) { Stop(); }
            }
            public void Stop()
            {
                tm.EraseTransient(plbox, new IntegerCollection());
                tm.EraseTransient(mt, new IntegerCollection());
                tm.EraseTransient(baifen, new IntegerCollection());
                tm.EraseTransient(line, new IntegerCollection());
            }
            MText Mtext(string textString, Point3d location, double height)
            {
                MText txt = new MText();
                txt.Location = location;
                txt.TextHeight = height;
                txt.Width = 0;
                txt.Rotation = 0;
                txt.Contents = textString;
                return txt;
            }
            Extents3d GetViewExtents3d()
            {
                ViewTableRecord vtr = Application.DocumentManager.MdiActiveDocument.Editor.GetCurrentView();
                double w = vtr.Width;
                double h = vtr.Height;
                Point2d pc = vtr.CenterPoint;
                Point3d pmin = new Point3d(pc.X - w / 2, pc.Y - h / 2, 0);
                Point3d pmax = new Point3d(pc.X + w / 2, pc.Y + h / 2, 0);
                Extents3d ext = new Extents3d(pmin, pmax);
                return ext;
            }

        }

    }
}
